lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var jobTable;
    table.set(common.layui.table.config());
    var STATUS_ENUM = {
        '0': '<span class="x-green">正常</span>'
        ,'1': '<span class="x-red">暂停</span>'
    }

    var OPNTION_ENUM = {
        'delete': '删除'
        ,'pause': '暂停'
        ,'resume': '恢复'
        ,'run': '立即执行'

    }

    // 渲染表格
    var renderTable = function (postData) {
        jobTable = table.render({
            elem: '#jobTable'
            , toolbar: '#toolbar'
            , url: common.util.createUrl('/sys/schedule/list')
            // 对分页请求的参数：page、limit重新设定名称
            , where: postData
            , cols: [[
                  {type:'checkbox'}
                , {type: 'numbers', title: '编号'}
                , {field: 'id',hide:true}
                , {field: 'beanName', title: 'Bean名称', minwidth: 80}
                , {field: 'params', title: '参数', minwidth: 80}
                , {field: 'cronExpression', title: 'cron表达式', minwidth: 80}
                , {field: 'remark', title: '备注', width: 240}
                , {field: 'status', title: '状态', width: 80, templet: function (row) {
                        var display = STATUS_ENUM[row.status + ''];
                        return display ? display : 'unknown';
                    }}
                , {
                    fixed: 'right', title: '操作', width: 320, templet: function (row) {
                        return '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update">修改</a>' +
                               '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>'+
                               '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="pause">暂停</a>'+
                               '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resume">恢复</a>'+
                               '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="run">立即执行</a>';
                    }
                }
            ]]


        });

        //监听单元格事件
        table.on('tool(jobTableFilter)', function(obj) {
            if (obj.event === 'update') {
                location.href = 'jobUpdate.html?id=' + obj.data.id;
            }else{
                unified(obj,obj.event)
            }
        });

        table.on('toolbar(jobTableFilter)', function(obj) {

            if (obj.event === 'add') {
                location.href = 'jobAdd.html';
            }else if(obj.event==="log"){
                pubPriKey(1,2);
            }else{
                var checkLines = table.checkStatus('jobTable')
                if(checkLines.data.length==0){
                    layer.msg('请选择操作数据',{icon:5,shift:6})
                    return ;
                }
                unified(checkLines,obj.event)
            }
        });
    };

    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });

    searchTable();

    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        if(!params){
            params=common.from.serializeObject('#searchFrm');
        }
        if (!jobTable) {
            jobTable = renderTable(params);
        } else {
            jobTable.reload({
                where: params
            })
        }
    }
    /**
     * 统一操作 删除,暂停,恢复,立即执行
     * @param obj
     */
    function unified(obj,event) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.jobId]
        }
        layer.confirm('确定对[id='+postData.join(',')+']进行['+OPNTION_ENUM[event]+']操作?', function(index){
            console.log('schedule '+event+' param is', postData);
            common.util.post('/sys/schedule/'+event, postData, function (resp) {
                layer.msg('操作成功',{icon:1,shift:6})
                searchTable();
                layer.close(index);
            })

        });
    }
    function pubPriKey(pubKey, priKey) {
        layer.open({
            type: 2,
            area: ['800px', '600px'],
            fix: false, //不固定
            shadeClose: true,
            shade:0.4,
            title: '任务日志',
            content: 'jobLogList.html'
        });
    }
});
